<?php
pc_base::load_app_class('hchbase','hchindex',0);
class detail extends hchbase {
	function __construct() {
		parent::__construct();
		$this->dbCrowdfunding = pc_base::load_model("mycrowdfunding_model");
		$this->dbComment = pc_base::load_model("mycomment_model");
        $this->dbJob = pc_base::load_model("myjob_model");
		$this->menuList = array(
			'userinfo'=>array('name'=>'个人信息','url'=>'/index.php?m=hchmember&c=detail&a=userinfo&action=userinfo','index'=>'userinfo'),
			'myproject'=>array('name'=>'个人项目','url'=>'/index.php?m=hchmember&c=detail&a=myproject&action=myproject&cf_type=1','index'=>'myproject'),
			'focusproject'=>array('name'=>'关注项目','url'=>'/index.php?m=hchmember&c=detail&a=myproject&action=focusproject&cf_type=2','index'=>'focusproject'),
			'job_list'=>array('name'=>'招聘管理','url'=>'/index.php?m=hchmember&c=detail&a=job_list&action=job_list','index'=>'job_list'),
		);
		
		$this->bulletInfo0 = '华创俱乐部 HCCLUB';
		$this->bulletInfo1 = '个人中心';
	}

	public function userinfo(){
        $action = $_GET['action']?$_GET['action']:'userinfo';
        $this->bulletInfo2 = $this->menuList[$action]['name'];
        $curURL = $this->menuList[$action]['url'];
		$userid = param::get_cookie('_userid');
		if(empty($userid)){
			showmessage('请登录','/index.php?m=hchmember&c=index&a=login');
		}else{
			$dbMember = pc_base::load_model('member_model');
			$dbMemberDetail = pc_base::load_model('mymemberdetail_model');
			$this->memberinfo = $dbMember->get_one(array('userid'=>$userid));
			$userinfo = $this->memberinfo;
			if($_GET['upgrade']){
                $dbMember->update(array('upgrade'=>1),$where = 'userid = '.$userid);
                $userinfo['upgrade'] = 1;
            }
			$years = array();
			for($i=1970;$i<2017;$i++){
				array_push($years, $i);
			}
			$month = array();
			for($i=1;$i<13;$i++){
				array_push($month, $i);
			}
			
			$detail = $dbMemberDetail->get_one(array('userid'=>$userid));
			
			if(!empty($detail['birthday'])){
				$user_year = date('Y',strtotime($detail['birthday']));
				$user_month = date('m',strtotime($detail['birthday']));
			}
			if(!empty($_POST['dosubmit'])){
				$editInfo = $_POST['item'];
				$year = $_POST['year'];
				$month = $_POST['month'];
				$birthday = date("Y-m-d",mktime(0,0,0,$month,0,$year));
				$editDetail = $_POST['detail'];
				$editDetail['birthday'] = $birthday;
                //更新头像
                if($_FILES['avatar']['size'] > 0) {
                    $attachment = pc_base::load_sys_class('attachment', '',1);
                    $upload_info = $attachment->upload('avatar');
                    if(!empty($upload_info)){
                        $attachment_db = pc_base::load_model('attachment_model');
                        $avatar_info = $attachment_db->get_one(array('aid'=>$upload_info[0]));
                        $editInfo['avatar'] = './uploadfile/'.$avatar_info['filepath'];
                    }
                }

				$dbMember->update($editInfo,$where = 'userid = '.$userid);
				$this->memberinfo = $dbMember->get_one(array('userid'=>$userid));
				if($detail){
					$dbMemberDetail->update($editDetail,$where = 'userid = '.$userid);
				}else{
					$editDetail['userid'] = $userid;
					$dbMemberDetail->insert($editDetail,true);
				}
				$detail = $dbMemberDetail->get_one(array('userid'=>$userid));
				$this->memberinfo = $dbMember->get_one(array('userid'=>$userid));
				$userinfo = $this->memberinfo;
			}
		}
        if($userinfo['groupid']){
            $member_group_db = pc_base::load_model('member_group_model');
            $member_group_info = $member_group_db->get_one(array('groupid'=>$userinfo['groupid']));
            $userinfo['group_name'] = $member_group_info['name'];
        }else{
            $userinfo['group_name'] = '普通会员';
        }
        include template('content/member', 'userdetail');
	}	
	
	public function startproject(){
        //省份列表
        $dbArea = pc_base::load_model("myarea_model");
        $where['area_level'] = 1;
        $area_list = $dbArea->select($where);

		$action = $_GET['action']?$_GET['action']:'myproject';
		$this->bulletInfo2 = $this->menuList[$action]['name'];
		$curURL = $this->menuList[$action]['url'];
		$userid = param::get_cookie('_userid');
		if(empty($userid)){
			showmessage('请登录','/index.php?m=hchmember&c=index&a=login');
		}else{
			if(!empty($_POST['dosubmit'])){
				$item = $_POST['item'];
				$item['is_show'] = 0;
                //地区保存
                if($_POST['aid_1']){
                    $item['aid'] = $_POST['aid_1'];
                }
                if($_POST['aid_2']){
                    $item['aid'] = $_POST['aid_2'];
                }
                if($_POST['aid_3']){
                    $item['aid'] = $_POST['aid_3'];
                }
				$editDetail['userid'] = $userid;
				$crowdfunding_id = $this->dbCrowdfunding->insert($item,true);
				
				$dbMember = pc_base::load_model('member_model');
				$this->memberinfo = $dbMember->get_one(array('userid'=>$userid));
				
				$u2c = array();
				$u2c['user_id'] = $this->memberinfo['userid'];
				$u2c['user_name'] = $this->memberinfo['username'];
				$u2c['crowdfunding_id'] = $crowdfunding_id;
				$u2c['crowdfunding_name'] = $item['name'];
				$u2c['amount'] = $item['amount'];
				$u2c['ratio'] = $item['ratio'];
				$u2c['relation_type'] = 1;
				$u2c['company'] = '';
				$dbU2C = pc_base::load_model('myuser2crowdfunding_model');
				$u2cId = $dbU2C->insert($u2c,true);
				if($u2cId>0){
					showmessage("新增项目成功！",$curURL);
				}else{
					showmessage("新增项目失败！");
				}
			}
		}
        
        $jintian = date('Ymd');
		include template('content/member', 'startproject');
	}
	
	public function myproject(){		
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$subId = $_GET['sub_id']?$_GET['sub_id']:0;
        $action = $_GET['action']?$_GET['action']:'myproject';
        $this->bulletInfo2 = $this->menuList[$action]['name'];
        $curURL = $this->menuList[$action]['url'];

        if($subId==0){
        	$userid = param::get_cookie("_userid");
        	if(!empty($userid)){
        		$crowdfundingType = $_GET['cf_type'];
        		
	        	$dbUser2Crowdfundging = pc_base::load_model("myuser2crowdfunding_model");
	        	$where = array('user_id'=>$userid,'relation_type'=>$crowdfundingType,'is_del'=>0);
				$inverstorList = $dbUser2Crowdfundging->select($where,'crowdfunding_id','');
				$crowdfundingIdStr = '';
				for($i=0;$i<count($inverstorList);$i++){
					if($i==0){
						$crowdfundingIdStr .= $inverstorList[$i]['crowdfunding_id'];
					}
					if($i > 0){
						$crowdfundingIdStr .= ','.$inverstorList[$i]['crowdfunding_id'];
					}
				}
				
				$whereSql = ' is_del = 0';
				if(!empty($crowdfundingIdStr)){
					$whereSql .= ' AND id IN ('.$crowdfundingIdStr.')';
			        $orderSql = " id DESC";
			        $lists = $this->dbCrowdfunding->listinfo($whereSql,$orderSql,$page,10);
				}
				$pages = $this->dbCrowdfunding->pages;
				include template ( 'common', 'page_list' );
	        }else{
        		header("Location: /");
        	}
        }else{
			$crowdfunding = $this->dbCrowdfunding->get_one(array('id'=>$subId),'*');
        	if(!empty($_GET['key'])){
        		$key = $_GET['key'];
        	}else{
        		$key = 'product_introduction';
        	}
        	$whereSql = array('id'=>$crowdfunding[$key]);
        	$info = $this->db->get_one($whereSql,'*');
        	if($key == 'product_introduction'){
	        	if(empty($info) || count($info)<=0 ){
	        		$info = $crowdfunding;
	        	}
        	}else{
        		if(empty($info) || count($info)<=0 ){
        			$info['name'] = $crowdfunding['name'];
        			$info['img'] = $crowdfunding['img'];
        		}
        	}
        	$infoId = $info['id'];
        	$browse = $info['browse_times'];
        	$isCrowdfunding = true;
        	$info['content'] = htmlspecialchars_decode($info['content']);
        	
        	$whereSql = array('project_id'=>$subId,'is_del'=>0,'comment_type'=>1);
        	$orderSql = " id DESC";
        	$commentLists = $this->dbComment->listinfo($whereSql,$orderSql,$page,10);
        	$pages = $this->dbComment->pages;
        	 
        	$dbUser2Crowdfundging = pc_base::load_model("myuser2crowdfunding_model");
        	$where = array('crowdfunding_id'=>$subId,'relation_type'=>1,'is_del'=>0);
			$inverstorList = $dbUser2Crowdfundging->select($where,'*','');
        	
			// print_r($crowdfunding);
        	include template ( 'content/crowdfunding', '_item' );
        }		
		
	}

    //地区联动
    public function get_area(){
        $aid = $_GET['aid'];
        $dbArea = pc_base::load_model("myarea_model");
        $area_info = $dbArea->get_one(array('id'=>$aid));

        $where['area_level'] = $area_info['area_level'] + 1;
        $where['parent_no'] = $area_info['area_no'];
        $list = $dbArea->select($where);
        echo json_encode($list);exit;
    }
    //申请关闭项目
    public function close_project(){
        $pid = $_GET['pid'];
        $res = $this->dbCrowdfunding->update(array('is_close'=>1),$where = 'id = '.$pid);
        $URL = '/index.php?m=hchmember&c=detail&a=myproject&action=myproject&cf_type=1';
        if($res){
            showmessage("申请已发送！",$URL);
        }else{
            showmessage("系统繁忙！");
        }
    }

    //招聘管理
    public function job_list(){
        $userid = param::get_cookie('_userid');
        $action=$_REQUEST['action'];
        $jobid=$_GET['jobid'];
        $is_show=$_GET['is_show'];
        $apply=$_GET['apply'];

        if(empty($userid)){
            showmessage('请先登录！','/index.php?m=hchmember&c=index&a=login');
        }else{
        if($action=='update'){
			if(!is_numeric($is_show) || !is_numeric($jobid)){showmessage('参数有误，请重试！','/index.php?m=hchmember&c=detail&a=job_list&action=job_list');}
            $item['is_show']=$is_show;
            $this->dbJob->update($item, $where = 'id = '.$jobid);
            showmessage('操作成功！','/index.php?m=hchmember&c=detail&a=job_list&action=job_list');
			exit;
        }elseif($action=='del'){
			if(!is_numeric($jobid)){showmessage('参数有误，请重试！','/index.php?m=hchmember&c=detail&a=job_list&action=job_list');}
            $this->dbJob->delete(array('id'=>$jobid)); 
            showmessage('删除成功！','/index.php?m=hchmember&c=detail&a=job_list&action=job_list');
			exit;
        }
        $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
        if($apply==1){ //应聘者列表
            $orderSql = ' `id` DESC';
            $whereSql = " (`company_id`={$userid}) ";
            $lists = $this->dbJob->listinfo($whereSql,$orderSql,$page,10);
            $count = $this->dbJob->count($whereSql); //统计当前记录总数
            $pages = $this->dbJob->pages;
            include template('hr', 'apply_list');
        }
        else{
            $orderSql = ' `id` DESC';
            $whereSql = " (`company_id`={$userid}) ";
            $lists = $this->dbJob->listinfo($whereSql,$orderSql,$page,10);
            $count = $this->dbJob->count($whereSql); //统计当前记录总数
            $pages = $this->dbJob->pages;
            include template('hr', 'job_list');
        }
        }
    }
    //应聘列表
    public function apply_list(){
        include template('hr', 'apply_list');
    }
    //简历管理
    public function resume(){
        include template('hr', 'resume');
    }
}
?>